// pages/driver_detail/driver_detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartnum: 1,
    loading:true,
    page:1,
    totalpage:100,
    loading:true,
    evaluateList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options:options
    })
    that.getData();
    that.getcomment();
  },
  getData:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/sys/info',
        data: {
          token:session,
          driverid:that.data.options.driverid
        },
        success:function(e){
          var res = e.data;
          if(res.msg="OK"){
            var data = res.data;
            that.setData({
              driver:data.driver,
              list:data.list,
              loading:false
            })
            if (parseInt(that.data.driver.rate)>5){
              that.data.driver.rate=5;
              that.setData({
                driver:that.data.driver
              })
            }
          }else{
            that.setData({
              loading:true,
              errorTips:"接口请求失败："+e.statusCode
            })
          }
        },
        fail:function(){
          that.setData({
            loading: true,
            errorTips: "接口请求失败：" + e.statusCode
          })
        }
      })
    })
  },
  //获取评论
  getcomment:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      if (that.data.page <= that.data.totalpage) {
        wx.request({
          url: app.api + '/wx_log_test/comment/driver/'+that.data.options.driverid,
          data: {
            token: session,
            page: that.data.page,
            perpage: 6
          },
          success: function (res) {
            if (res.statusCode == 200) {
              if (res.data.msg == 'OK') {
                if (res.data.data.totalElements == 0) {
                  that.setData({
                    loadtips: '暂无相关评论',
                    loading: false
                  })
                } else {
                  that.setData({
                    evaluateList: that.data.evaluateList.concat(res.data.data.content),
                    totalpage: res.data.data.totalPages,
                    loading: false
                  })
                  console.log(that.data.evaluateList);
                  if (that.data.page == that.data.totalpage) {
                    that.setData({
                      loadtips: '暂无更多评论',
                      page: ++that.data.page
                    })
                  } else {
                    that.setData({
                      page: ++that.data.page
                    })
                  }
                }
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                })
              }
            } else {
              wx.showModal({
                title: '提示',
                content: "接口返回错误，状态码：" + res.statusCode,
              })
            }
          }
        })
      }
    })
  },
  lower:function(res){
    this.getcomment();
  },
  onShareAppMessage: function () {
  
  },
  //派发订单
  appoint:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api +'/wx_log_test/order/appoint?token='+session+'&&cargoid='+that.data.options.cargoid+'&&driverid='+that.data.options.driverid,
        method:'POST',
        success:function(res){
          var res = res.data;
          if(res.msg=="OK"){
            wx.showToast({
              title: '派单成功',
              icon:'success',
              duration:1500,
              success:function(res){
                wx.navigateBack({
                  delta:1
                })
              }
            })
          }else{
            wx.showToast({
              title: '接口请求错误',
              icon: 'loading'
            })
          }
        },
        fail:function(res){
          wx.showToast({
            title: '接口请求错误',
            icon: 'loading'
          })
        }
      })
    })
  }
})